[contents] [prev] [next] [top] [bottom] (6 out of 12)

Flow of Control

An if expression evaluates a simple conditional to determine flow of control in a program. In ScriptX, a conditional may yield any object, with the Boolean false object meaning false, and any other value meaning true. The if expression has three alternate forms, based on whether an else clause is supplied. If then is supplied as the second keyword, without an accompanying else clause, execution is delayed until the parser interprets the next complete expression. To force evaluation in a listener window after a then clause, enter two exclamation marks ( !! ).

ifExpr	::=	if simpleExpr then expr else expr 
	|	if simpleExpr do expr 
	|	if simpleExpr then expr 

The ScriptX case expression is analogous to the switch statement in C and C++, but the ScriptX case expression is more flexible. As with other control structures, a case expression first evaluates a simple control expression. Case tags can be any factor. These factors are evaluated in order. When a factor matches the value of the control expression (using the equals global function, equivalent to the "=" operator, as a test), its associated expression is executed. If no factor matches, the expression in the otherwise clause, if one is provided, is executed. In contrast with the C switch statement, no explicit break is required. Only one expression within the body of a case expression is executed.

caseExpr	::=	case [ simpleExpr ] of taggedFormList end 
taggedFormList	::=	taggedForm [ moreTaggedForms ] 
moreTaggedForms	::=	[ endOfLine ]* taggedFormList 
taggedForm	::=	factor : expr 
	|	otherwise : expr 

The repeat expression is almost identical with the if . . . do form of the if expression, except that the target expression is repeated as a loop until its control expression returns false. The first two forms of this expression do not guarantee that the loop will ever be executed, since the control expression might return false on its first iteration. The final two forms of this expression guarantee that the loop will be executed at least once, since the control expression is evaluated only after the loop is executed.

repeatExpr	::=	repeat while simpleExpr do expr 
	|	repeat until simpleExpr do expr
	|	repeat expr while simpleExpr 
	|	repeat expr until simpleExpr 

The ScriptX for expression has three parts. The first part gives the source or sources of iteration. The second part, which is optional, provides a conditional that is evaluated once before each iteration of the loop. The third part is the body of the loop.

forExpr	::=	for forSources [ forTest ] forBody 

A for expression can supply more than one source of iteration. The program iterates through these sources in parallel, stopping when the first of these iterations ends.

forSources	::=	forSource [ , forSource ]* 
forSource	::=	simpleExpr 
	|	symbol := simpleExpr 
	|	symbol in simpleExpr 

There are two forms for the source of iteration in a for expression. The simplest takes a simple expression yielding a number. It uses this number as a count for the number of times the loop should iterate. The second associates a series of values with a local variable, accessible in the loop body. This form can be used with the discrete range literal, or with a simple expression that yields a collection.

Syntax for the optional test clause in a for expression is the same as for a repeat expression.

forTest	::=	while simpleExpr 
	|	until simpleExpr 

The body of a for expression is a clause that is repeated on each iteration of the loop. Three variants are available for the body of a for expression. In the first variant, the body of the loop is simply an expression, often a compound expression, that is evaluated once for each iteration of the loop. The second and third variants result in a collection of values that is built during the iterations. The third variant selects values for this collection based on the result of a test expression.

forBody	::=	do expr 
	|	collect [ into simpleExpr ] [ by factor ] \
			[ as factor ] simpleExpr 
	|	select factor [ into simpleExpr ] \
			[ by factor ] [ as factor ] if simpleExpr 

The second and third variants for the body of a for expression allow for any of three optional clauses, which must specified in the order shown. The into clause allows a script to specify an existing collection as the target of collect or select. The by clause allows a script to specify the collection function that will be used to add values to the collection (appendReturningSelf, a variant of append that returns a collection, is the default). The as clause specifies which class of collection should be constructed.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.